import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class TodoListApp extends JFrame {
    private List<Todo> todos;
    private JTable todoTable;
    private DefaultTableModel tableModel;
    private JTextField titleField;
    private JSpinner dateSpinner;

    public TodoListApp() {
        todos = new ArrayList<>();
        
        // 设置窗口
        setTitle("待办事项清单");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建输入面板
        JPanel inputPanel = new JPanel(new FlowLayout());
        titleField = new JTextField(20);
        SpinnerDateModel dateModel = new SpinnerDateModel();
        dateSpinner = new JSpinner(dateModel);
        JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(dateSpinner, "yyyy-MM-dd");
        dateSpinner.setEditor(dateEditor);
        
        JButton addButton = new JButton("添加");
        addButton.addActionListener(e -> addTodo());

        inputPanel.add(new JLabel("标题:"));
        inputPanel.add(titleField);
        inputPanel.add(new JLabel("日期:"));
        inputPanel.add(dateSpinner);
        inputPanel.add(addButton);

        // 创建表格
        String[] columnNames = {"标题", "日期", "状态", "操作"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        todoTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(todoTable);

        // 添加删除按钮列
        todoTable.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer());
        todoTable.getColumnModel().getColumn(3).setCellEditor(new ButtonEditor(new JCheckBox()));

        // 添加组件到主面板
        mainPanel.add(inputPanel, BorderLayout.NORTH);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        add(mainPanel);
    }

    private void addTodo() {
        String title = titleField.getText().trim();
        if (title.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入标题");
            return;
        }

        LocalDate date = ((java.util.Date) dateSpinner.getValue()).toInstant()
            .atZone(java.time.ZoneId.systemDefault())
            .toLocalDate();

        Todo todo = new Todo(title, date);
        todos.add(todo);
        updateTable();
        
        titleField.setText("");
    }

    private void updateTable() {
        tableModel.setRowCount(0);
        for (Todo todo : todos) {
            tableModel.addRow(new Object[]{
                todo.getTitle(),
                todo.getDate(),
                todo.isCompleted() ? "已完成" : "未完成",
                "删除"
            });
        }
    }

    private class ButtonRenderer extends JButton implements javax.swing.table.TableCellRenderer {
        public ButtonRenderer() {
            setOpaque(true);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            setText((value == null) ? "" : value.toString());
            return this;
        }
    }

    private class ButtonEditor extends DefaultCellEditor {
        private JButton button;
        private int clickedRow;

        public ButtonEditor(JCheckBox checkBox) {
            super(checkBox);
            button = new JButton();
            button.setOpaque(true);
            button.addActionListener(e -> fireEditingStopped());
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
            clickedRow = row;
            button.setText((value == null) ? "" : value.toString());
            return button;
        }

        @Override
        public Object getCellEditorValue() {
            todos.remove(clickedRow);
            updateTable();
            return "删除";
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new TodoListApp().setVisible(true);
        });
    }
} 